package com.itheima.mall.api.ums;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.ums.UmsRoleMapper;
import com.itheima.mall.pojo.ums.UmsRole;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.R;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@DubboService
public class UmsRoleServiceApiImpl implements UmsRoleServiceApi{
    @Autowired
    private UmsRoleMapper umsRoleMapper;
    /**
     * 根据角色id查找角色详情
     * @param roleId
     * @return
     */
    @Override
    public UmsRole getUserRole(Long roleId) {
        UmsRole umsRole = umsRoleMapper.selectById(roleId);
        return umsRole;
    }

    @Override
    public R UmsRoleCreate(UmsRole umsRole) {
        umsRole.setCreateTime(LocalDateTime.now());

        int flag = umsRoleMapper.insert(umsRole);

        if (flag>0){
            return R.succeed("","创建成功");
        }

        return R.error("创建失败");
    }

    @Override
    public R UmsRoleDelete(Long[] ids) {

        int flag = umsRoleMapper.deleteBatchIds(Arrays.asList(ids));

        if (flag>0){
            return R.succeed("","删除成功");
        }

        return R.error("删除失败");
    }

    @Override
    public R UmsRoleList(String keyword, Integer pageNum, Integer pageSize) {

        Page pageCondition = new Page(Convert.toLong(pageNum),Convert.toLong(pageSize));
        LambdaQueryWrapper<UmsRole> lqw = new LambdaQueryWrapper<>();
        lqw.like(ObjectUtil.isNotNull(keyword),UmsRole::getName,keyword);

        Page pageR = umsRoleMapper.selectPage(pageCondition, lqw);
        PageResult pageResult = new PageResult();
        pageResult.setList(pageR.getRecords());
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setTotalPage(Convert.toInt(pageR.getPages()));
        pageResult.setTotal(Convert.toInt(pageR.getTotal()));

        return R.succeed(pageResult,"查询成功");
    }

    @Override
    public R UmsRoleListAll() {
        LambdaQueryWrapper<UmsRole> lqw = new LambdaQueryWrapper();
        List<UmsRole> umsRoles = umsRoleMapper.selectList(lqw);
        if (ObjectUtil.isNotNull(umsRoles)){
            return R.succeed(umsRoles,"查找成功");
        }
        return R.error("查找失败");
    }

    @Override
    public R UmsRoleUpdate(Long id, UmsRole umsRole) {
        umsRole.setId(id);
        int flag = umsRoleMapper.updateById(umsRole);
        if (flag>0){
            return R.succeed("","更新成功");
        }

        return R.error("更新失败");
    }

    @Override
    public R UmsRoleUpdateStatus(Long id, Integer status) {
        UmsRole umsRole = new UmsRole();
        umsRole.setId(id);
        umsRole.setStatus(status);

        int flag = umsRoleMapper.updateById(umsRole);

        if (flag>0){
            return R.succeed("","更新成功");
        }

        return R.error("更新失败");
    }

}
